package tp.pr4.instructions;

import java.util.StringTokenizer;
import tp.pr4.NavigationModule;
import tp.pr4.RobotEngine;
import tp.pr4.instructions.exceptions.*;
import tp.pr4.items.ItemContainer;

/**
 * This instruction drops an Item from the current place and puts it the 
 * robot inventory. This instruction works if the user writes DROP or SOLTAR
 * @author Alvaro Saez Hernando
 * @author Noel Clemente Montero 
 */
public class DropInstruction implements Instruction{
	
	private static final String LINE_SEPARATOR = System.lineSeparator();
	
	private String id;
	private NavigationModule navigation;
	private ItemContainer robotContainer;
	
	/**
	 * Default constructor.
	 */
	public DropInstruction(){
		this.id=null;
	}
	
	/**
	 * Creates a DropInstruction using a ID 
	 * @param id
	 */
	private DropInstruction(String id){
		this.id=id;
	}
	/**
	 * Parses the String returning an instance of DropInstruction or throwing a 
	 * WrongInstructionFormatException()
	 * @param cad text String to parse
	 * @return Instruction Reference to an instance of DropInstruction
	 * @throws WrongInstructionFormatException When the String is not  DROP <id>
	 */
	public Instruction parse(String cad) throws WrongInstructionFormatException {
		
		StringTokenizer st = new StringTokenizer(cad, " ");
		String word = st.nextToken().toUpperCase();
		
		if ((word.equals("DROP")) || (word.equals("SOLTAR"))) {
			if (st.hasMoreTokens()) {
				String id = st.nextToken();
				if (!st.hasMoreTokens())
					return new DropInstruction(id);
				else
					throw new WrongInstructionFormatException("WALL·E says: I do not understand. Please repeat" + LINE_SEPARATOR);
			} 
			else
				throw new WrongInstructionFormatException("WALL·E says: I do not understand. Please repeat" + LINE_SEPARATOR);
		} 
		else
			throw new WrongInstructionFormatException("WALL·E says: I do not understand. Please repeat" + LINE_SEPARATOR);
	}

	/**
	 * Instruction help
	 * @return the instruction syntax 
	 */
	public String getHelp() {
		return "DROP | SOLTAR <id>" + LINE_SEPARATOR;
	}

	/**
	 * Fixes the current context, i.e., the robot engine and the navigation module
	 * @param engine The robot engine
	 * @param navigation The information about the game, i.e., the places, current 
	 * direction and current heading to navigate
	 * @param robotContainer The inventory of the robot
	 */
	public void configureContext(RobotEngine engine,NavigationModule navigation, ItemContainer robotContainer) {
		
		this.navigation = navigation;
		this.robotContainer = robotContainer;
		
	}

	/**
	 * The robot drops an Item from its inventory in the current place, if the item exists
	 * @throws InstructionExecutionException When the robot inventory does not contain 
	 * an item with this name or when there is another item with the same id in the current place
	 */
	public void execute() throws InstructionExecutionException {
		
		if (id != null && robotContainer.containsItem(id)){
			
			if (!navigation.findItemAtCurrentPlace(id))
			{				
				navigation.getCurrentPlace().addItem(robotContainer.pickItem(id));
				System.out.print("Great! I have dropped "+this.id + LINE_SEPARATOR);
			} 
			else throw new InstructionExecutionException("WALL·E says: I am stupid! The Place already has the object " + id + LINE_SEPARATOR);
		}
		else throw new InstructionExecutionException("You do not have any " + id + "." + LINE_SEPARATOR);
		
		
		
	}
	
}


